package com.yonyou.gmmc.service.pointactivity.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.FrameHttpUtil;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.service.pointactivity.constant.PointActivityConstant;
import com.yonyou.gmmc.service.pointactivity.service.PointActivityService;

import io.swagger.annotations.Api;

/*
 * 积分活动
 */
@RestController
@RequestMapping(value = PointActivityConstant.BASE_PATH + "/" + PointActivityConstant.VERSION)
@Api("积分活动")
public class PointActivityController {

	private Logger logger = Logger.getLogger(PointActivityController.class);

	@Autowired
	private PointActivityService pointActivityService;

	/**
	 * 进入页面初始化页面时,调用的方法
	 * 
	 * @param req
	 * @return Map
	 */
	@RequestMapping(value = "/initPage", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> initPage(HttpServletRequest req) {

		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
		// String openId = "oXrcfuC1R4wosEPeJOHR7BPqaAOo";
		logger.info("积分活动获取到的的openId为:" + openId);

		// openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;

		// logger.info("积分活动获取到的的openId为==========:" + openId);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			resultMap = pointActivityService.getResultMap(openId);
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, "积分活动异常");
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}

		return resultMap;

	}

	/**
	 * 获取积分奖励,调用的方法
	 * 
	 * @param req
	 * @return Map
	 */
	@RequestMapping(value = "/getPointReward", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public synchronized @ResponseBody Map<String, Object> getPointReward(HttpServletRequest req) {

		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
		// String openId = "oXrcfuC1R4wosEPeJOHR7BPqaAOo";
		logger.info("积分活动获取到的的openId为:" + openId);

		// openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;

		// logger.info("积分活动获取到的的openId为==========:" + openId);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			resultMap = pointActivityService.getPointReward(openId);
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, "积分抽奖异常");
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}

		return resultMap;

	}

	@RequestMapping(value = "/sendMaxTurntable", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody String sendMaxTurntable(@RequestParam("openId") String openId,@RequestParam("integral") String integral) {

		//String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);

		logger.info("积分活动获取到的的openId为:" + openId);
        logger.info("积分活动获取到的的integral为:" + integral);

		//String integral = isNull(req.getParameter("")) ? "0" : req.getParameter("");

		Map<String, String> param = new HashMap<String, String>();
		param.put("openid", openId);
		param.put("integral", integral);
		param.put("archiveId", "SlyderAdventures");

		return  FrameHttpUtil.post(PointActivityConstant.slyderAdventuresURL, param);
	}
	
	public static boolean isNull(String str){
		return (str== null || "".equals(str.trim()));
	}

}